home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / LOC / LOCCOUNT.C < prev    next >
Text File  |  1991-11-20  |  7KB  |  296 lines

  1. /* LOCCounter.c -- window methods */
  2. /* Created 10/4/91 10:31 AM by AppMaker */
  3.  
  4. /*    This module overrides the AppMaker-generated code in zLOCCounter.        */
  5. /*    It provides a place for you to add your own code and still be able to    */
  6. /*    generate code for new changes to the user interface.  This module will    */
  7. /*    not be regenerated by AppMaker unless you delete it.  Its superclass,    */
  8. /*    zLOCCounter, may be regenerated to handle user interface changes        */
  9. /*    without losing your hand-coded changes to this module.                    */
  10.  
  11. #include <CBureaucrat.h>
  12. #include <Commands.h>
  13. #include <TBUtilities.h>
  14. #include <DialogAids.h>
  15. #include "ResourceDefs.h"
  16. #include "LOCCounter.h"
  17.  
  18. pascal int FileFilter ();
  19. pascal int dHook ();
  20. void SpinCursor ( void );
  21.  
  22. extern Cursor waitCursor[6];    // cursors for spinning
  23. extern CBureaucrat *gGopher;    // The current boss in the chain of command.
  24.  
  25. long        whichDir;            // Dir ID to start looking in
  26. int            whichVol;            // Vol RefNum of same
  27. char        name[40];            // scratch folder name
  28. char        fname[40];            // current file name
  29. Str255        saveName;            // save file name
  30. int            vRef1, saveRef;        // volume ref & file ref of save file
  31. int            cursorCount;        // what cursor were we on last time?
  32. SFReply        frep;                // Reply from file selector.
  33.  
  34. TMetricData    **metricData;                    // Metric information collected by the tool.
  35. Boolean        analyzeSubdirectories = false;    // True if subdirectories are to be analyzed as well.
  36.  
  37. /*----------*/
  38. void CLOCCounter::ILOCCounter ( CDirector *aSupervisor )
  39.  
  40. BEGIN
  41.   inherited::ILOCCounter ( aSupervisor );
  42.   itsGopher = this;
  43.  
  44.   // Get the previous metric data from the application's resource file.
  45.  
  46.   metricData = (TMetricData    **) GetResource ( 'DATA', 128 );
  47.   LoadResource ( metricData );
  48.   HLock ( metricData );
  49.  
  50.   // Initialize the various window fields.
  51.  
  52.   this->IMetricFields ( *metricData );
  53.  
  54.   HUnlock ( metricData );
  55. END
  56.  
  57. /*----------*/
  58. void CLOCCounter::IMetricFields ( TMetricData *data )
  59.  
  60. BEGIN
  61.   Str255 text;
  62.  
  63.   NumToString ( data->numLines, text );
  64.   numLOCNowLabel->SetTextString ( text );
  65.   NumToString ( data->numSemis, text );
  66.   numSemicolonsNowLabel->SetTextString ( text );
  67.   NumToString ( data->numClasses, text );
  68.   numClassesNowLabel->SetTextString ( text );
  69.  
  70.   NumToString ( data->prevLines, text );
  71.   numLOCPrevLabel->SetTextString ( text );
  72.   NumToString ( data->prevSemis, text );
  73.   numSemicolonsPrevLabel->SetTextString ( text );
  74.   NumToString ( data->prevClasses, text );
  75.   numClassesPrevLabel->SetTextString ( text );
  76.  
  77.   NumToString ( data->deltaLines, text );
  78.   numLOCDeltaLabel->SetTextString ( text );
  79.   NumToString ( data->deltaSemis, text );
  80.   numSemicolonsDeltaLabel->SetTextString ( text );
  81.   NumToString ( data->deltaClasses, text );
  82.   numClassesDeltaLabel->SetTextString ( text );
  83. END
  84.  
  85. /*----------*/
  86. /* Prompt the user on where to start indexing through folders to create the report */
  87.  
  88. static Boolean GetStartDir ( void )
  89.  
  90. BEGIN
  91.     Point pt;
  92.  
  93.     whichDir = 0;
  94.     whichVol = 0;
  95.     FindDlogPosition( 'DLOG', getDlgID, &pt );
  96.     SFGetFile ( pt, "\p", &FileFilter, 0, 0L, &dHook, &frep );
  97.     return frep.good;
  98. END
  99.  
  100. /*----------*/
  101. /* File filter for standard file. Always returns TRUE so only folders will be displayed. */
  102.  
  103. static pascal int FileFilter ( void *Pparms )
  104.  
  105. BEGIN
  106.   return (-1);
  107. END
  108.  
  109. /*----------*/
  110. /* Dialog hook routine so the "this folder" button will work */
  111.  
  112. static pascal int dHook ( int item, DialogPtr dp )
  113.  
  114. BEGIN
  115.   IF item == 11 THEN
  116.     whichDir = CurDirStore;
  117.     whichVol = SFSaveDisk * -1;
  118.     frep.good = true;
  119.     return(3);
  120.  
  121.   ELSIF item == 13 THEN
  122.     analyzeSubdirectories = NOT analyzeSubdirectories;
  123.     SetCheckbox ( 13, analyzeSubdirectories );
  124.  
  125.   END_IF;
  126.   return(item);
  127. END
  128.  
  129. /*----------*/
  130. /* Put up the next cursor */
  131.  
  132. static void SpinCursor ( void )
  133.  
  134. BEGIN
  135.   if (++cursorCount > 5) cursorCount = 0;
  136.   SetCursor( &waitCursor[cursorCount] );
  137. END
  138.  
  139. /*----------*/
  140. void CLOCCounter::AnalyzeFile ( long dirID, TMetricData *data )
  141.  
  142. BEGIN
  143.     int            fileRefNum;
  144.     long        bytes;
  145.     char        buffer[256];
  146.     int            handDelay;
  147.     const int    initHandDelay = 100;
  148.  
  149.     // Perform metric calculations.
  150.  
  151.     IF NOT HOpen ( whichVol, dirID, fname, fsRdPerm, &fileRefNum ) THEN
  152.  
  153.       // Increment the files counter.
  154.               
  155.       data->numClasses++;
  156.  
  157.       // Test the contents of the file.
  158.  
  159.       handDelay = initHandDelay;
  160.       bytes = 1;
  161.       WHILE FSRead ( fileRefNum, &bytes, &buffer ) == noErr LOOP
  162.  
  163.         // Provide feedback that something is happening.
  164.  
  165.         IF NOT --handDelay THEN
  166.           handDelay = initHandDelay;
  167.           SpinCursor();
  168.         END_IF;
  169.  
  170.         // Test for line terminator.
  171.              
  172.         IF buffer[0] == '\r' THEN
  173.           data->numLines++;
  174.         END_IF;
  175.  
  176.         // Test for semicolon.
  177.  
  178.         IF buffer[0] == ';' THEN
  179.           data->numSemis++;
  180.         END_IF;
  181.  
  182.       END_LOOP;
  183.       FSClose ( fileRefNum );
  184.     END_IF;
  185. END
  186.  
  187. /*----------*/
  188. void CLOCCounter::AnalyzeDirectory ( long dirID, int index, TMetricData *data )
  189.  
  190. BEGIN
  191.     CInfoPBRec    myParam;
  192.  
  193.     // Analyze the contents of the files within the directory.
  194.  
  195.     WHILE true LOOP
  196.       myParam.dirInfo.ioCompletion = 0;
  197.       myParam.dirInfo.ioNamePtr = (StringPtr) &fname;
  198.       myParam.dirInfo.ioVRefNum = whichVol;
  199.       myParam.dirInfo.ioDrDirID = dirID;
  200.       myParam.dirInfo.ioFDirIndex = index++;
  201.       if (PBGetCatInfo(&myParam, 0)) break;
  202.       IF (myParam.dirInfo.ioFlAttrib & 16) AND analyzeSubdirectories THEN
  203.         this->AnalyzeDirectory ( myParam.dirInfo.ioDrDirID, 1, data );
  204.       ELSIF fname[fname[0]-1] == '.' AND
  205.           (fname[fname[0]] == 'c' OR
  206.           fname[fname[0]] == 'C' OR
  207.           fname[fname[0]] == 'h' OR
  208.           fname[fname[0]] == 'H') THEN
  209.         this->AnalyzeFile ( dirID, data );
  210.       END_IF;
  211.     END_LOOP;
  212. END
  213.  
  214. /*----------*/
  215. void CLOCCounter::CountMetrics ( long dirID, int index )
  216.  
  217. BEGIN
  218.     TMetricData    *data;
  219.  
  220.     // Initialize the metric information.
  221.  
  222.     LoadResource ( metricData );
  223.     HLock ( metricData );
  224.     data = *metricData;
  225.     data->numClasses = 0;
  226.     data->numLines = 0;
  227.     data->numSemis = 0;
  228.  
  229.     // Analyze the contents of the files.
  230.  
  231.     this->AnalyzeDirectory ( dirID, 1, data );
  232.  
  233.     // Compute the difference between the current metrics and the previous.
  234.  
  235.     IF data->metricsBaselined THEN
  236.       data->deltaClasses = data->numClasses - data->prevClasses;
  237.       data->deltaLines = data->numLines - data->prevLines;
  238.       data->deltaSemis = data->numSemis - data->prevSemis;
  239.     END_IF;
  240.  
  241.     // Update the display.
  242.  
  243.     this->IMetricFields ( data );
  244.  
  245.     HUnlock ( metricData );
  246.     ChangedResource ( metricData );
  247.     WriteResource ( metricData );
  248. END
  249.  
  250. /*----------*/
  251. void CLOCCounter::DoCommand ( long theCommand )
  252.  
  253. BEGIN
  254.   Str255 text;
  255.  
  256.   switch (theCommand) {
  257.  
  258.     case cmdSelectFolder:
  259.       BEGIN
  260.         IF GetStartDir () THEN
  261.           CountMetrics ( whichDir, 1);
  262.         END_IF;
  263.       END;
  264.       break;
  265.  
  266.     case cmdBaselineMetrics:
  267.       BEGIN
  268.         TMetricData    *data;
  269.  
  270.         LoadResource ( metricData );
  271.         HLock ( metricData );
  272.         data = *metricData;
  273.  
  274.         data->prevClasses = data->numClasses;
  275.         data->deltaClasses = 0;
  276.         data->prevLines = data->numLines;
  277.         data->deltaLines = 0;
  278.         data->prevSemis = data->numSemis;
  279.         data->deltaSemis = 0;
  280.         data->metricsBaselined = true;
  281.  
  282.         this->IMetricFields ( data );
  283.  
  284.         HUnlock ( metricData );
  285.         ChangedResource ( metricData );
  286.         WriteResource ( metricData );
  287.       END;
  288.       break;
  289.  
  290.     default:
  291.       inherited::DoCommand (theCommand);
  292.       break;
  293.   }
  294.  
  295. }
  296.